#include"ThreadCache.h"

void* ThreadCache::FetchFromCentralCache(int index, size_t alignSize)
{
	///...
	return nullptr;
}

void* ThreadCache::Allocate(size_t size)
{
	assert(size <= Max_Size);
	size_t alignSize = SizeClass::RoundUp(size);
	size_t index = SizeClass::Index(size);

	//先去自由链表的哈希桶找是否有资源
	if (!_freelists[index].Empty())
	{
		return _freelists[index].Pop();
	}
	//没有的话再去找中心缓存
	else
	{
		return FetchFromCentralCache(index, alignSize);
	}
}

void ThreadCache::Deallocate(void* ptr, size_t size)
{
	assert(ptr);
	assert(size <= Max_Size);

	//找到目标大小映射的自由链表桶,进行插入
	size_t index = SizeClass::Index(size);
	_freelists[index].Push(ptr);
}